<?php 
require_once('SQLAuthenticationHandler.class.php');
class MySQLAuthenticationHandler extends  SQLAuthenticationHandler 
{
    
        /**
     * A database connection.
     *
     * @access protected
     * @since  2.0
     * @type   resource
     */
    var $conn;


    /**
     * Create a new MySQLAuthenticationHandler instance.
     *
     * @access public
     * @since  2.0
     */
    function MySQLAuthenticationHandler($params)
    {

        $this->conn       =  NULL;

        // set default parameters
        $defparams             = array();
        $defparams['table']    = 'users';
        $defparams['username_col'] = 'username';
        $defparams['password_col']   = 'passwd';

        // merge parameters
        $params = array_merge($defparams, $params);
    }

    function login (&$controller,&$user){
        
        $credential =$user->getCredential();
        //$credential =new BasicCredential();
        $u = $credential->getUsername();
        $p = $credential->getPassword();
        if (!$this->conn==NULL){
            $this->conn->SetFetchMode(2);
            $sqlLogin = "SELECT * FROM USERS WHERE username='".$u."' and passwd='".$p."'";
            $rsLogin =  $this->conn->Execute($sqlLogin);
            if ($rsLogin){
               $UserData=$rsLogin->FetchRow();   
               if ($UserData){         
                    $keys=array_keys($UserData);
                    $values=array_values($UserData);
                    $nkeys=sizeof($keys);
                    for ($i=0;$i<$nkeys;$i++){
                       if ($keys[$i]!="passwd"){
                           $user->setAttribute($keys[$i],$values[$i]);
                       }
                    }
                    return TRUE;
               }
            }else{
//             $error = 'SQLAuthenticationHandler::logout(&$controller,&$user): Error Executing SQL ';
//             trigger_error($error, E_USER_ERROR);
             return false;
            }
                        
        }else{
             $error = 'Connection Instance cannot be NULL: ';
             trigger_error($error, E_USER_ERROR);
        }
        return FALSE;
    }
    
    function logout (&$controller,&$user){
        $error = 'SQLAuthenticationHandler::logout(&$controller,&$user) must be overridden';

        trigger_error($error, E_USER_ERROR);

        exit;        
        
    }
/**
     * Close the session.
     *
     * <br/><br/>
     *
     * <note>
     *     This should never be called manually.
     * </note>
     *
     * @access public
     * @since  2.0
     */
    function close ()
    {

        if ($this->conn != NULL)
        {

            if (@mysql_close($this->conn) === FALSE)
            {

                $error = 'Failed to close MySQL session -- error: ' .
                         mysql_errno($this->conn) . ':' .
                         mysql_error($this->conn);

                trigger_error($error, E_USER_ERROR);

                return FALSE;

            }

        }

        return TRUE;

    }
    
    /**
     * Retrieve the number of rows from a result set.
     *
     * @param resource A result set.
     *
     * @return int The number of rows in a result set.
     *
     * @access public
     * @since  2.0
     */
    function getNumRows (&$result)
    {

        $error = 'SQLAuthenticationHandler::getNumRows(&$result) must be overridden';

        trigger_error($error, E_USER_ERROR);

        exit;

    }

    /**
     * Set the database connection.
     *
     * @param resource A database connection.
     *
     * @access public
     * @since  2.0
     */
    function setConnection (&$conn)
    {

        $this->conn =& $conn;

    }
  /**
     * Open the session.
     *
     * <br/><br/>
     *
     * <note>
     *     This should never be called manually.
     * </note>
     *
     * @param path A session path.
     * @param name A session name.
     *
     * @access public
     * @since  2.0
     */
    function open ($path, $name)
    {

        if ($this->conn == NULL)
        {

            $function = ($this->persistent)
                        ? 'mysql_pconnect' : 'mysql_connect';

            if (isset($this->params['host']))
            {

                // host exists
                if (isset($this->params['user']))
                {

                    // user exists
                    if (isset($this->params['password']))
                    {

                        // password exists

                        // connect with host, user, password
                        $this->conn = $function($this->params['host'],
                                                 $this->params['user'],
                                                 $this->params['password']);

                    } else
                    {

                        // connect with host, user
                        $this->conn = $function($this->params['host'],
                                                 $this->params['user']);

                    }

                } else
                {

                    // connect with host
                    $this->conn = $function($this->params['host']);

                }

            } else
            {

                // connect with no params
                $this->conn = $function();

            }

            if ($this->conn !== FALSE)
            {

                if (!isset($this->params['db']))
                {

                    $error = 'Please specify a MySQL database';

                    trigger_error($error, E_USER_ERROR);

                    return FALSE;

                } else if (@mysql_select_db($this->params['db'], $this->conn) === FALSE)
                {

                    $error = 'Failed to select database -- reason: ' .
                             mysql_errno($this->conn) . ':' .
                             mysql_error($this->conn);

                    trigger_error($error, E_USER_ERROR);

                    return FALSE;

                }

            } else
            {

                $this->conn = NULL;

            }

        }

        return TRUE;

    }  

}

?>